package com.zp.crm.service.PubService.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zp.crm.dao.PubDao.PubDao;
import com.zp.crm.dto.pub.PubDto;
import com.zp.crm.entity.Pub.PubEntity;
import com.zp.crm.service.PubService.PubService;
import com.zp.crm.taskls.Timetask.TakEntity;
import com.zp.crm.util.GrouoBy.Gro;
import com.zp.crm.util.targetLog.Log;
@Service("pubService")
public class PubServiceImpl implements PubService {
	
	@Resource
	public PubDao pubDao;
	@Log(name="根据编号，名称查询公共数据，分页并封装成html形式")
	@Override
	public Map<String, Object> select(String code, String name, int page, int size) {
		Map<String, Object> map=new HashMap<>();
		
		int PageIndex=(page-1)*size;
		
		int count=pubDao.selectcount(code,name);
		
		List<PubEntity> list=pubDao.select(code,name,PageIndex,size);
		
		String html="";
		
		if(code!=null && !code.equals("")&&name != null && !name.equals("")){
			html=new PubDto().html(count, list, page, 1);
		}else{
			html=new PubDto().html(count, list, page, 0);
		}
		
		map.put("message", html);
		return map;
	}
	@Log(name="添加数据字典公共数据 ")
	@Override
	public Map<String, Object> insert(PubEntity entity) {
		Map<String, Object> map=new HashMap<>();

		Map<String, Object> t=SelectByTypeNameYz(entity.getCode(),entity.getName());

		if(!(boolean) t.get("success")){
			int r=pubDao.insert(entity);
			if(r>0){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="修改数据字典公共数据")
	@Override
	public Map<String, Object> edit(PubEntity entity) {
		Map<String, Object> map=new HashMap<>();
		Map<String, Object> t=SelectByTypeNameYz(entity.getCode(),entity.getName());
		
		if(!(boolean) t.get("success")){
			int r=pubDao.edit(entity);
			if(r>0){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		return map;
	}
	@Log(name="删除数据字典公共数据 ")
	@Override
	public Map<String, Object> delete(int id) {
		Map<String, Object> map=new HashMap<>();

		int r=pubDao.delete(id);
		
		if(r>0){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="根据类型查询公共数据 ")
	@Override
	public Map<String, Object> SelectCode(String code) {
		
		Map<String, Object> map=new HashMap<>();
		
		List<PubEntity> list=pubDao.SelectCode(code);
		
		String selecthtml=new PubDto().SelectHtml(list);
		
		map.put("message", selecthtml);
		
		return map;
	}
	@Log(name="sql查询统计信息 ")
	@Override
	public Map<String, Object> SelectGy(String type, String from) {
		Map<String, Object> map=new HashMap<>();
		String sql="";
		if(from.equals("crm_dd")){
			sql="select "+type+" 'type',SUM(ddje) 'mun' from "+from+" GROUP BY code order by mun desc";
		}else{
			sql="select "+type+" 'type',count(*) 'mun' from "+from+" GROUP BY "+type+" order by mun desc";
		}
		List<Gro> list=pubDao.SelectGy(sql);
		
		String tablehtml=new PubDto().GyHtml(list);
		
		map.put("message", tablehtml);
		
		return map;
	}
	@Log(name="根据类型名称查询是否重复 ")
	@Override
	public Map<String, Object> SelectByTypeNameYz(String type, String name) {
		Map<String, Object> map=new HashMap<>();

		int r=pubDao.SelectByTypeNameYz(type,name);
		
		if(r>0){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="根据Id查询 ")
	@Override
	public Map<String, Object> SelectById(int id) {
		Map<String, Object> map=new HashMap<>();

		PubEntity entity=pubDao.SelectById(id);
		
		if(entity != null){
			map.put("success", true);
			map.put("message", entity);
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="定时器修改客户状态 ")
	@Override
	public int LsUpdate(List<TakEntity> ulist, String string) {
		return pubDao.LsUpdate(ulist,string);
	}
	@Log(name="定时器流失添加")
	@Override
	public int LsInsert(List<TakEntity> ulist) {
		return pubDao.LsInsert(ulist);
	}
	@Log(name="定时器查询数据")
	@Override
	public List<TakEntity> LsSelect(String sql) {
		return pubDao.LsSelect(sql);
	}

}
